import {defineStore} from "pinia"
import defaultSettings from "../settings"
import en from "element-plus/es/locale/lang/en"
import zhCn from "element-plus/es/locale/lang/zh-cn"

export default defineStore("app", {
    state() {
        const language = defaultSettings.language
        const sidebarStatus = "opened"
        const sidebar = {
            opened: sidebarStatus !== "closed",
            withoutAnimation: false,
        }
        const theme = 1
        const count = 0

        const studioEnable = (import.meta.env.VITE_APP_STUDIO_ENABLE === 'true')

        return {
            studioEnable,
            currentWhName: "",
            currentWhId: "",
            theme,
            count,
            language,
            sidebar,
            sidebarStatus
        }
    },
    getters: {
        /**
         * 根据语言标识读取对应的语言包
         */
        locale(state) {
            if (state?.language == "en") {
                return en;
            } else {
                return zhCn;
            }
        }
    },
    actions: {
        loadFromIndexDb(state) {
            delete state.studioEnable
            this.$patch(state)
        },
        changeLanguage(val: string) {
            this.language = val;
        },
        changeCurrentWh(whId: string, whName: string) {
            this.currentWhId = whId;
            this.currentWhName = whName;
        },
        toggleSidebar(withoutAnimation: boolean) {
            this.sidebar.opened = !this.sidebar.opened;
            this.sidebar.withoutAnimation = withoutAnimation;
            if (this.sidebar.opened) {
                this.sidebarStatus = "opened";
            } else {
                this.sidebarStatus = "closed";
            }
        },
        closeSideBar(withoutAnimation: boolean) {
            this.sidebar.opened = false;
            this.sidebar.withoutAnimation = withoutAnimation;
            this.sidebarStatus = "closed";
        },
        openSideBar(withoutAnimation: boolean) {
            this.sidebar.opened = true;
            this.sidebar.withoutAnimation = withoutAnimation;
            this.sidebarStatus = "opened";
        }
    }
})

// export const useAppStore = defineStore("app", () => {
//
//
//     const language = useStorage("language", defaultSettings.language);
//     const sidebarStatus = useStorage("sidebarStatus", "closed");
//     const sidebar = reactive({
//         opened: sidebarStatus.value !== "closed",
//         withoutAnimation: false,
//     });
//
//     /**
//      * 根据语言标识读取对应的语言包
//      */
//     const locale = computed(() => {
//         if (language?.value == "en") {
//             return en;
//         } else {
//             return zhCn;
//         }
//     });
//
//     /**
//      * 切换语言
//      *
//      * @param val
//      */
//     function changeLanguage(val: string) {
//         language.value = val;
//     }
//
//     function toggleSidebar(withoutAnimation: boolean) {
//         sidebar.opened = !sidebar.opened;
//         sidebar.withoutAnimation = withoutAnimation;
//         if (sidebar.opened) {
//             sidebarStatus.value = "opened";
//         } else {
//             sidebarStatus.value = "closed";
//         }
//     }
//
//     function closeSideBar(withoutAnimation: boolean) {
//         sidebar.opened = false;
//         sidebar.withoutAnimation = withoutAnimation;
//         sidebarStatus.value = "closed";
//     }
//
//     function openSideBar(withoutAnimation: boolean) {
//         sidebar.opened = true;
//         sidebar.withoutAnimation = withoutAnimation;
//         sidebarStatus.value = "opened";
//     }
//
//     return {
//         // 语言
//         language,
//         locale,
//         changeLanguage,
//
//         // 侧边栏
//         sidebar,
//         toggleSidebar,
//         closeSideBar,
//         openSideBar,
//     }
// })
